Page({
  data: {
    coupons: [
      { id: 1, name: '8折', discount: 0.8, selected: false }
    ]
  },

  selectCoupon(e) {
    const index = e.currentTarget.dataset.index;
    const coupons = this.data.coupons.map((item, i) => {
      if (i === index) {
        // 切换选中状态
        item.selected = !item.selected;
      }
      return item;
    });
    this.setData({ coupons });
    
    const selectedCoupon = coupons.find(item => item.selected);
    if (selectedCoupon) {
      wx.setStorageSync('selectedCoupon', selectedCoupon);
    } else {
      wx.removeStorageSync('selectedCoupon');
    }
    
    // 获取上一个页面实例
    const pages = getCurrentPages();
    if (pages.length > 1) {
      const prevPage = pages[pages.length - 2];
      if (prevPage.route === 'pages/confirmOrder/confirmOrder') {
        // 调用上一个页面的 onShow 方法
        prevPage.onShow();
      }
    }
    // 返回上一个页面
    wx.navigateBack();
  },

  onLoad(options) {

  },

  onReady() {

  },

  onShow() {
    const selectedCoupon = wx.getStorageSync('selectedCoupon');
    const coupons = this.data.coupons.map(item => {
      if (selectedCoupon && item.id === selectedCoupon.id) {
        item.selected = true;
      } else {
        item.selected = false;
      }
      return item;
    });
    this.setData({ coupons });
  },

  onHide() {

  },

  onUnload() {

  },

  onPullDownRefresh() {

  },

  onReachBottom() {

  },

  onShareAppMessage() {

  }
});